//
//  MMReleaseViewController.m
//  Gunz
//
//  Created by Joggy on 16/5/4.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "MMReleaseViewController.h"
#import "GZConst.h"
#import "MMReleaseTableView1.h"
#import "MMReleaseTableView2.h"

@interface MMReleaseViewController ()<UIScrollViewDelegate> {
    
    UISegmentedControl *navSegment;
    BOOL navSegmentAdded;
    UIScrollView *scroll;
    
}

@end

@implementation MMReleaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    navSegmentAdded = false;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(!navSegmentAdded) {
        navSegment = [[UISegmentedControl alloc] initWithItems:@[@"正在执行", @"已完成"]];
        navSegment.frame = CGRectMake(0, 0, 160, 32);
        navSegment.layer.position = CGPointMake(screenWidth/2, 22);
        navSegment.tintColor = CUSTOM_GREEN;
        navSegment.selectedSegmentIndex = 0;
        [navSegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        [self.navigationController.navigationBar addSubview:navSegment];
        navSegmentAdded = true;
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if(navSegment != nil) {
        [UIView animateWithDuration:0.3 animations:^{
            navSegment.alpha = 0;
        } completion:^(BOOL finished) {
            [navSegment removeFromSuperview];
            navSegmentAdded = false;
        }];
    }
}

- (void)prepareForNav {
    [super prepareForNav];
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64)];
    scroll.contentSize = CGSizeMake(screenWidth*2, screenHeight - 64);
    scroll.pagingEnabled = true;
    scroll.showsVerticalScrollIndicator = false;
    scroll.showsHorizontalScrollIndicator = false;
    scroll.delegate = self;
    [self.view addSubview:scroll];
    [self addChildViewController:[[MMReleaseTableView1 alloc] init]];
    [self addChildViewController:[[MMReleaseTableView2 alloc] init]];
    UIView *tableView1 = self.childViewControllers[0].view;
    [scroll addSubview:tableView1];
    UIView *tableView2 = self.childViewControllers[1].view;
    tableView2.frame = CGRectMake(screenWidth, 0, tableView2.frame.size.width, tableView2.frame.size.height);
    [scroll addSubview:tableView2];
}

#pragma 事件方法

- (void)segmentAction:(UISegmentedControl*)seg {
    [scroll scrollRectToVisible:CGRectMake(seg.selectedSegmentIndex*screenWidth, 0, scroll.frame.size.width, scroll.frame.size.height) animated:true];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma 代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    navSegment.selectedSegmentIndex = scrollView.contentOffset.x/screenWidth;
}

@end
